﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UserLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        bool Authenticated = false;
        Authenticated = SiteLevelCustomAuthenticationMethod(UserName.Text, Password.Text);
        if (Authenticated == true)
        {
            string strConnection = "workstation id=MiniSaaS.mssql.somee.com;packet size=4096;user id=SaaS_cse564;pwd=123abccse;data source=MiniSaaS.mssql.somee.com;persist security info=False;initial catalog=MiniSaaS";
            System.Data.SqlClient.SqlConnection Connection = new System.Data.SqlClient.SqlConnection(strConnection);
            String strSQL = "Select Login_ID From LOGIN WHERE Login_Name = '" + UserName.Text + "';";
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(strSQL, Connection);
            System.Data.SqlClient.SqlDataReader Dr;
            Connection.Open();
            Dr = command.ExecuteReader();
            Dr.Read();
            int currentID = (int)Dr[0];
            Session["LoginID"] = currentID;

            Response.Redirect("ControlPanel.aspx");
        }
    }

        private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
        {
            bool boolReturnValue = false;
            string strConnection = "workstation id=MiniSaaS.mssql.somee.com;packet size=4096;user id=SaaS_cse564;pwd=123abccse;data source=MiniSaaS.mssql.somee.com;persist security info=False;initial catalog=MiniSaaS";
            System.Data.SqlClient.SqlConnection Connection = new System.Data.SqlClient.SqlConnection(strConnection);
            String strSQL = "Select * From LOGIN WHERE Login_Name = '" + UserName + "'and Login_Password='" + Password + "';";
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(strSQL, Connection);
            System.Data.SqlClient.SqlDataReader Dr;
            Connection.Open();
            Dr = command.ExecuteReader();
            if (Dr.HasRows)
            {

                boolReturnValue = true;
            }
            else
            {
                boolReturnValue = false;
            }
            Connection.Close();
            Dr.Close();
            return boolReturnValue;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlConnection con;
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = "workstation id=MiniSaaS.mssql.somee.com;packet size=4096;user id=SaaS_cse564;pwd=123abccse;data source=MiniSaaS.mssql.somee.com;persist security info=False;initial catalog=MiniSaaS";
            con.Open();
            System.Data.SqlClient.SqlCommand cmd;
            //String strCommand = "INSERT INTO META (Table_ID, Table_Version, Login_ID, Table_Name, Table_Columns_Count, Table_Columns, Table_Description) VALUES (1, 2, 4, 'Product', 5,'<columns><col><name>m_price</name><type>double</type><allownull>false</allownull><default>0.0</default></col><col><name>m_producer</name><type>string</type><allownull>true</allownull></col><col><name>m_pDate</name><type>date</type><allownull>true</allownull></col><col><name>m_eDate</name><type>date</type><allownull>true</allownull></col><col><name>m_description</name><type>string</type><allownull>false</allownull></col></columns>', 'Parent class for all products')";
            string strCommand = "INSERT INTO DATA (Data_ID, Table_ID, Table_Version, Data_Rows, Data_Comment) VALUES (2, 1, 1,'<data><col><coldata>1.0</coldata></col><col><coldata>AMD</coldata></col><col><coldata>10/8/2004</coldata></col><col><coldata>11/24/2005</coldata></col><col><coldata>Processor</coldata></col></data>', NULL);";
            cmd = new System.Data.SqlClient.SqlCommand(strCommand, con);
            int rows = cmd.ExecuteNonQuery();
            con.Close();
        }
}

